<template>
  <div class="config-page">
    <div class="page-header">
      <button class="back-button" @click="goToHome">
        <span class="icon-arrow-left"></span>
        返回首页
      </button>
      <h1>SLA配置</h1>
    </div>
    
    <div class="page-content">
      <div class="config-header">
        <p>设置和管理服务级别协议，确保工单处理符合服务承诺</p>
        <button class="primary-btn" @click="showAddModal = true">
          <i class="icon-plus"></i> 新增SLA规则
        </button>
      </div>
      
      <div class="card">
        <div class="search-bar">
          <input 
            type="text" 
            v-model="searchKeyword" 
            placeholder="搜索SLA规则名称..."
            class="search-input"
          >
          <button class="search-btn" @click="searchSLA">搜索</button>
        </div>
        
        <table class="config-table">
          <thead>
            <tr>
              <th>规则名称</th>
              <th>响应时间</th>
              <th>解决时间</th>
              <th>优先级</th>
              <th>状态</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="sla in filteredSLAs" :key="sla.id">
              <td>{{ sla.name }}</td>
              <td>{{ sla.responseTime }}分钟</td>
              <td>{{ sla.resolveTime }}小时</td>
              <td>
                <span :class="['priority-tag', 'priority-' + sla.priority]">
                  {{ sla.priority === 'high' ? '高' : sla.priority === 'medium' ? '中' : '低' }}
                </span>
              </td>
              <td>
                <span :class="['status-tag', sla.active ? 'status-active' : 'status-inactive']">
                  {{ sla.active ? '启用' : '禁用' }}
                </span>
              </td>
              <td class="action-buttons">
                <button class="edit-btn" @click="editSLA(sla)">编辑</button>
                <button class="delete-btn" @click="deleteSLA(sla.id)">删除</button>
              </td>
            </tr>
          </tbody>
        </table>
        
        <div class="empty-state" v-if="filteredSLAs.length === 0">
          <i class="icon-empty"></i>
          <p>暂无SLA规则，请点击新增按钮创建</p>
        </div>
      </div>
    </div>
    
    <!-- 新增/编辑SLA模态框 -->
    <div class="modal-overlay" v-if="showAddModal || showEditModal">
      <div class="modal">
        <div class="modal-header">
          <h3>{{ showEditModal ? '编辑SLA规则' : '新增SLA规则' }}</h3>
          <button class="close-btn" @click="closeModal">&times;</button>
        </div>
        <div class="modal-body">
          <form class="sla-form">
            <div class="form-group">
              <label>规则名称 *</label>
              <input 
                type="text" 
                v-model="currentSLA.name" 
                class="form-control"
                required
              >
            </div>
            <div class="form-group">
              <label>响应时间(分钟) *</label>
              <input 
                type="number" 
                v-model="currentSLA.responseTime" 
                class="form-control"
                min="1"
                required
              >
            </div>
            <div class="form-group">
              <label>解决时间(小时) *</label>
              <input 
                type="number" 
                v-model="currentSLA.resolveTime" 
                class="form-control"
                min="1"
                required
              >
            </div>
            <div class="form-group">
              <label>优先级 *</label>
              <select v-model="currentSLA.priority" class="form-control" required>
                <option value="high">高</option>
                <option value="medium">中</option>
                <option value="low">低</option>
              </select>
            </div>
            <div class="form-group">
              <label class="checkbox-label">
                <input 
                  type="checkbox" 
                  v-model="currentSLA.active"
                >
                启用该规则
              </label>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button class="cancel-btn" @click="closeModal">取消</button>
          <button class="primary-btn" @click="saveSLA">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SLAConfig',
  data() {
    return {
      searchKeyword: '',
      showAddModal: false,
      showEditModal: false,
      currentSLA: {
        id: null,
        name: '',
        responseTime: 30,
        resolveTime: 24,
        priority: 'medium',
        active: true
      },
      slas: [
        {
          id: 1,
          name: 'VIP客户紧急响应',
          responseTime: 15,
          resolveTime: 4,
          priority: 'high',
          active: true
        },
        {
          id: 2,
          name: '普通客户标准响应',
          responseTime: 60,
          resolveTime: 48,
          priority: 'medium',
          active: true
        },
        {
          id: 3,
          name: '试用客户基础响应',
          responseTime: 120,
          resolveTime: 72,
          priority: 'low',
          active: true
        }
      ]
    };
  },
  computed: {
    filteredSLAs() {
      if (!this.searchKeyword) return this.slas;
      const keyword = this.searchKeyword.toLowerCase();
      return this.slas.filter(sla => 
        sla.name.toLowerCase().includes(keyword)
      );
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    searchSLA() {
      // 搜索逻辑已在计算属性中实现
    },
    editSLA(sla) {
      this.currentSLA = { ...sla };
      this.showEditModal = true;
    },
    deleteSLA(id) {
      if (confirm('确定要删除这条SLA规则吗？')) {
        this.slas = this.slas.filter(sla => sla.id !== id);
      }
    },
    closeModal() {
      this.showAddModal = false;
      this.showEditModal = false;
      this.resetCurrentSLA();
    },
    resetCurrentSLA() {
      this.currentSLA = {
        id: null,
        name: '',
        responseTime: 30,
        resolveTime: 24,
        priority: 'medium',
        active: true
      };
    },
    saveSLA() {
      if (!this.currentSLA.name || !this.currentSLA.responseTime || !this.currentSLA.resolveTime) {
        alert('请填写必填字段');
        return;
      }
      
      if (this.showEditModal) {
        // 更新现有SLA
        const index = this.slas.findIndex(sla => sla.id === this.currentSLA.id);
        if (index !== -1) {
          this.slas.splice(index, 1, this.currentSLA);
        }
      } else {
        // 添加新SLA
        this.currentSLA.id = Date.now();
        this.slas.push({ ...this.currentSLA });
      }
      
      this.closeModal();
    }
  }
}
</script>

<style scoped>
.config-page {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e1e5e8;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

.page-content {
  margin-top: 20px;
}

.config-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.config-header p {
  color: #666;
  font-size: 14px;
}

.card {
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  padding: 20px;
}

.search-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.search-input {
  flex: 1;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
}

.search-btn {
  padding: 10px 20px;
  background: #f5f7fa;
  border: 1px solid #ddd;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.2s;
}

.search-btn:hover {
  background: #e9ecef;
}

.config-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

.config-table th,
.config-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #f0f0f0;
}

.config-table th {
  background-color: #f9fafb;
  font-weight: 600;
  color: #333;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.edit-btn, .delete-btn {
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.2s;
  border: none;
}

.edit-btn {
  background-color: #e3f2fd;
  color: #1976d2;
}

.edit-btn:hover {
  background-color: #bbdefb;
}

.delete-btn {
  background-color: #ffebee;
  color: #d32f2f;
}

.delete-btn:hover {
  background-color: #ffcdd2;
}

.priority-tag {
  padding: 3px 8px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.priority-high {
  background-color: #ffebee;
  color: #d32f2f;
}

.priority-medium {
  background-color: #fff8e1;
  color: #f57c00;
}

.priority-low {
  background-color: #e8f5e9;
  color: #388e3c;
}

.status-tag {
  padding: 3px 8px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.status-active {
  background-color: #e8f5e9;
  color: #388e3c;
}

.status-inactive {
  background-color: #f5f5f5;
  color: #757575;
}

.empty-state {
  text-align: center;
  padding: 40px 0;
  color: #999;
}

.icon-empty::before {
  content: "📁";
  font-size: 48px;
  display: block;
  margin-bottom: 15px;
}

.primary-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  font-weight: 500;
}

.primary-btn:hover {
  background: #2980b9;
}

.icon-plus::before {
  content: "+";
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background-color: white;
  border-radius: 10px;
  width: 500px;
  max-width: 90%;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.modal-header {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #999;
}

.modal-body {
  padding: 20px;
}

.sla-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-group label {
  font-weight: 500;
  font-size: 14px;
}

.form-control {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.modal-footer {
  padding: 15px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.cancel-btn {
  padding: 8px 15px;
  background: #f5f5f5;
  border: 1px solid #ddd;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.2s;
}

.cancel-btn:hover {
  background: #e9e9e9;
}

.icon-arrow-left::before {
  content: "←";
}
</style>